Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix aggregate cursor als #15094

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

IchirokuXVI
Copy link

Summary

Aggregate cursors didn't automatically add session if it was present in AsyncLocalStorage. Also the options weren't the same for exec and cursor so I added a function to make them the same just like in QueryCursor.

Examples

const mongoose = require("mongoose");
mongoose.set("transactionAsyncLocalStorage", true);

mongoose.connect("mongodb://127.0.0.1:27017/cursortest");

const test = async () => {
  await mongoose.connection.transaction(async (session) => {
    const TestModel = mongoose.model(
      "Test",
      new mongoose.Schema({ name: String })
    );

    mongoose.set("debug", true);
    await TestModel.aggregate([{ $match: {} }]);
    const cursor1 = TestModel.aggregate([{ $match: {} }]).cursor();
    const cursor2 = TestModel.aggregate([{ $match: {} }])
      .session(session)
      .cursor();

    await cursor1.next();
    await cursor2.next();
    mongoose.set("debug", false);

    /*
        From the debug logs:
            Mongoose: TestModel.aggregate([ { '$match': {} } ], { session: ClientSession("bb843cfeb01d4ec3ad0373d2b7d3db20") })
            Mongoose: TestModel.aggregate([ { '$match': {} } ], { cursor: {}, session: ClientSession("bb843cfeb01d4ec3ad0373d2b7d3db20")})
            Mongoose: TestModel.aggregate([ { '$match': {} } ], { session: ClientSession("bb843cfeb01d4ec3ad0373d2b7d3db20"), cursor: {}})

        Before the fix the first cursor didn't have a session but aggregate and second cursor had a session.
    */
  });
};

test()
  .then(() => console.log("Finished"))
  .catch((error) => console.error(error))
  .finally(() => process.exit(0));

@IchirokuXVI IchirokuXVI force-pushed the fix-aggregate-cursor-als branch from 46fc7ed to 51e6b08 Compare December 13, 2024 17:09
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a couple of minor style suggestions, but looks good overall.

lib/aggregate.js Outdated
*/

Aggregate.prototype._optionsForExec = function() {
const options = clone(this.options || {});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clone() shouldn't be necessary here, would be ideal to avoid cloning for performance purposes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous implementation also used clone(), so I retained it to ensure no existing functionality was unintentionally affected. However, I agree that removing clone() could improve performance. If it’s deemed unnecessary, I suggest calling the function directly in the constructor to initialize the options instead.

@@ -57,21 +58,25 @@ util.inherits(AggregationCursor, Readable);
function _init(model, c, agg) {
if (!model.collection.buffer) {
model.hooks.execPre('aggregate', agg, function() {
Object.assign(c.options, agg._optionsForExec());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to previous comments, is there any need to set c.options at all? Could just call aggregate(agg._pipeline, agg.optionsForExec())

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In QueryCursor, options is also set to a new object, so I followed the same approach here.
That said, QueryCursor uses the options object for some checks while AggregateCursor only uses it in _init, so it might make sense to remove it from AggregateCursor for now.

@IchirokuXVI
Copy link
Author

Removed the cloning in _optionsForExec and also sest the options before creating AggregationCursor in Aggregate.cursor(). Passed all tests with npm test.

@IchirokuXVI IchirokuXVI force-pushed the fix-aggregate-cursor-als branch from 6ec8cd5 to 4b8a25e Compare December 16, 2024 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants